package com.xx.controller;

import com.github.pagehelper.PageInfo;
import com.xx.entity.R;
import com.xx.entity.Suppliers;
import com.xx.enums.HttpEnum;
import com.xx.service.ISuppliersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/suppliers")
@CrossOrigin //解决跨域问题
public class SuppliersController {
    @Autowired
    ISuppliersService suppliersService;

    @GetMapping
    public R queryByPage(Suppliers suppliers, Integer pageNum) {
        PageInfo<Suppliers> pageInfo = suppliersService.queryByPage(suppliers,pageNum);
        return R.success(pageInfo);
    }

    //通过ID查询
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") Integer id) {
        Suppliers suppliers = suppliersService.queryById(id);
        if (suppliers.getSupplierName() != null) {
            return R.success(suppliers);
        }
        return R.fail(HttpEnum.WARN.getCode(),"没有该供应商");
    }

    //新增
    @PostMapping
    public R add(@RequestBody Suppliers suppliers) {
        if (suppliersService.insert(suppliers)>0){
            return R.success(suppliers);
        }
        return R.fail(HttpEnum.WARN.getCode(),"添加失败");
    }

    //修改
    @PutMapping
    public R edit(@RequestBody Suppliers suppliers) {
        if (suppliersService.update(suppliers)>0){
            return R.success(suppliers);
        }
        return R.fail(HttpEnum.WARN.getCode(),"编辑失败");
    }

    //删除
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        if (suppliersService.deleteById(id)>0){
            return R.success(true);
        }
        return R.fail(HttpEnum.WARN.getCode(),"删除失败");
    }
    @GetMapping("/list")
    public R list() {
        return R.success(suppliersService.queryAll());
    }
}
